<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<title>title</title>
		<script type="text/javascript" src="../script/api.js"></script>
		<script type="text/javascript" src="../script/config.js"></script>
		<script type="text/javascript" src="../script/jquery-2.1.3.js"></script>
		<script type="text/javascript" src="../script/av-0.5.0.min.js"></script>
		<script type="text/javascript" src="../script/mico-ac-0.2.js"></script>
		<script type="text/javascript" src="../script/jquery.md5.js"></script>
		<style>
			body {
				font-size: 15px;
				color: green;
				font-family: '黑体';
			}
			input {
				width: 100px;
				height: 20px;
				border: 1px solid #99D3F5;
				border-radius: 4px;
			}
			.ipbutton {
				width: 100px;
				height: 25px;
				background: #D0EEFF;
				border: 1px solid #99D3F5;
				border-radius: 4px;
				color: #B7197C;
			}
			.title {
				font-weight: bolder;
				color: #D23434;
			}
		</style>
	</head>
	<body>
		<br/>
		<br/>
		<a href="./deviceManage.html">返回玩设备</a>
		<br/>
		<br/>
		<span class="title">控制设备只有很简单的几步：</span>
		<br/>
		1、先点击
		<input class="ipbutton" type="button" onclick="startMqttService()" value="打开通道"/>
		，建立起连接，这时候设备应该开始说话了。
		<br/>
		2、试着控制下，我先点
		<input class="ipbutton" type="button" onclick="publishCommand(1)" value="开灯"/>
		，再点
		<input class="ipbutton" type="button" onclick="publishCommand(0)" value="关灯"/>
		，是不是看到效果了？
		<br/>
		3、用完了，记得点击
		<input class="ipbutton" type="button" onclick="stopMqttService()" value="关闭通道"/>
		<br/>
		<br/>
		设备说：<span id="devsay"></span>
	</body>
	<script type="text/javascript">
		apiready = function() {
		};
		
		function startMqttService() {
			var userToken = MiCO.getUserToken();
			var device_id = "d64f517c/c8934691813c";
			var host = "api.easylink.io";
			var username = "admin";
			var password = "admin";
			//clientID，需要按照此标准来定义：v1-app-[token	版本号-app-userToken(前12位)
			var clientID = "v1-app-" + userToken.substring(0, 12);
			var topic = device_id + '/out/#';
			MiCO.startMqttService(host, username, password, topic, clientID, function(ret, err) {
				$("#devsay").text(JSON.stringify(ret));
			});
		}

		function stopMqttService() {
			MiCO.stopMqttService();
		}

		function publishCommand(type) {
			var device_id = "d64f517c/c8934691813c";
			var command;
			if (1 == type) {
				command = '{"4":true}';
			} else if (0 == type) {
				command = '{"4":false}';
			}
			var topic = device_id + "/in/write";
			MiCO.publishCommand(topic, command, function(ret, err) {
				$("#devsay").text(JSON.stringify(ret));
			});
		}
	</script>
</html>